<template>
    <div class='approval'>
        <el-card class="box-card">
            <div class='title'>
                {{$t('approvalDispatch.title1')}}
            </div>
            <div class='menu-search'> 
                <div class='menu-title'>
                  <!-- 时间 -->
                  <div class='searchItem searchItem-date'>
                    <span>{{$t('approvalDispatch.crtTime')}}</span>
                    <el-date-picker
                    size='small'
                    v-model="listQuery.startDate"
                    type="date"
                    value-format="yyyy-MM-dd"
                    :placeholder="$t('rule.beginDate')">
                    </el-date-picker>
                    <b>{{$t('approval.go')}}</b>
                    <el-date-picker
                    size='small'
                      v-model="listQuery.endDate"
                      type="date"
                      value-format="yyyy-MM-dd"
                      :placeholder="$t('rule.endDate')">
                    </el-date-picker>
                  </div>
                  <!-- 业务渠道 -->
                  <div class='searchItem'>
                    <span>{{$t('customerService.channel')}}</span>
                    <el-select v-model='listQuery.channel' size='small' @change="handleChannelChange">
                      <el-option
                      v-for='item in channelList'
                      :key='item.channel'
                      :label='item.org_name'
                      :value='item.channel'>
                      </el-option>
                    </el-select>
                  </div>
                  <!-- 业务ID -->
                  <div class='searchItem'>
                      <span>{{$t('customerService.orderId')}}</span>
                      <el-input v-model='listQuery.orderId' :placeholder="$t('rule.orderId')" size='small'></el-input>
                  </div>
                  <!-- 手机号 -->
                  <div class='searchItem'>
                    <span>{{$t('customerService.phoneNo')}}</span>
                    <el-input v-model='listQuery.userPhone' :placeholder="$t('rule.phoneNo')" size='small'></el-input>
                  </div>
                  <!-- 客户姓名 -->
                  <div class='searchItem'>
                    <span>{{$t('customerService.customerName')}}</span>
                    <el-input v-model='listQuery.userName' :placeholder="$t('rule.realName')" size='small'></el-input>
                  </div>    
                </div>
                <div class='menu-title-btn'>
                  <button type='button' @click='search'>
                      {{$t('btn.query')}}
                  </button>
                  <button type='button' @click='reset'>
                      {{$t('btn.reset')}}
                  </button>
              </div>
            </div>
        </el-card>
        <el-card class='box-card' style="margin-top: 10px;margin-bottom: 20px;">
            <div class='title'>
                {{$t('approvalDispatch.title2')}}
            </div>
            <div class='menu-list'>
              <div>
                <el-button type="text" @click="assignedTo">{{$t('approvalDispatch.assignTo')}}</el-button>
              </div>
            </div>
            <el-table
                v-loading="listLoading"
                ref='multipleTable'
                :data='tableData'
                tooltip-effect='dark'
                style='width: 100%'
                @selection-change='handleSelectionChange'
                border
                size='mini'>
                <el-table-column
                header-align='center'
                align='center'
                type='selection'
                width='55'>
                </el-table-column>
                <!-- 业务id -->
                <el-table-column
                prop='orderId' align="center" header-align="center" 
                :label="$t('customerService.orderId')"
                width='140'>
                </el-table-column>
                <!-- 审批额度 -->
                <el-table-column
                prop='loanAmt' align="center" header-align="center" 
                :label="$t('customerService.apprAmt')"
                :formatter="cashFormatter"
                width='180'>
                </el-table-column>
                <!-- 放款金额 -->
                <el-table-column
                prop='lentAmt' align="center" header-align="center" 
                :label="$t('customerService.loanAmt')"
                :formatter="cashFormatter"
                width='180'>
                </el-table-column>
                <!-- 放款成功时间 -->
                <el-table-column
                prop='cashoutDate' align="center" header-align="center" 
                :label="$t('customerService.successTime')"
                width='140'>
                </el-table-column>
                <!-- 借款期限 -->
                <el-table-column
                prop='pTerm' align="center" header-align="center" 
                :label="$t('customerService.loanTerm')"
                width='100'>
                </el-table-column>
                <!-- 客户姓名 -->
                <el-table-column
                prop="userRealName" align="center" header-align="center" 
                :label="$t('customerService.customerName')"
                width='200'>
                </el-table-column>
                <!-- 手机号 -->
                <el-table-column
                prop='userPhone' align="center" header-align="center" 
                :label="$t('customerService.phoneNo')"
                width='200'>
                </el-table-column>
                <!-- 借款App -->
                <el-table-column
                prop='appPackageHeader' align="center" header-align="center" 
                :label="$t('approvalDetail.loanApp')"
                width='140'>
                </el-table-column>
                <!-- 业务渠道 -->
                <el-table-column
                prop='channelDesc' align="center" header-align="center" 
                :label="$t('customerService.channel')"
                width='200'>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
              <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="listQuery.page"
              :page-sizes="[5, 10, 20, 30]"
              :page-size="listQuery.limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
              </el-pagination>
            </div>
        </el-card>
        <el-dialog :title="$t('customerService.pdfp')" :visible.sync="dialogFormVisible">
          <div class='handler'>
              <span>{{$t('customerService.lqr')}}</span>
              <el-select v-model='serviceId' :placeholder="$t('rule.choose')" size='small'>
                  <el-option
                  v-for='(item, index) in serviceIds'
                  :key='index'
                  :label='item.value'
                  :value='item.label'>
                  </el-option>
              </el-select>
          </div>
          <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible = false">{{$t('btn.cancel')}}</el-button>
              <el-button type="primary" @click="confirmFn">{{$t('btn.confirm')}}</el-button>
          </div>
        </el-dialog>
    </div>
</template>

<script>
import { mapGetters } from 'vuex';
import { formatCash } from 'utils/index'
import { getChannels, getCustomer, sendOrder, page } from '@/api/customerService/index'
export default {
  name: 'UnDistributedOrder',
  data() {
    return {
      listLoading: false,
      total: null,
      listQuery: {
        limit: 5,
        page: 1,
        // 分配状态 1:未分配2：已分配
        distributionStatus: 1,
        // 放款开始时间
        startDate: '',
        // 放款结束时间
        endDate: '',
        // 渠道
        channel: '',
        // 订单id
        orderId: '',
        // ⽤用户⼿手机号
        userPhone: '',
        // 客户姓名
        userName: '',
        // 客服组id
        customGroupId: '',
        // serviceGroupId: '',
        // 客服员id
        customId: '',
        // 处理理状态
        status: '',
        // 近期数据类型
        type: ''
      },
      channelList: [],
      tableData: [],
      multipleSelection: [],
      dialogFormVisible: false,
      // 客服⼈列表
      serviceIds: [],
      // 客服员id
      serviceId: ''
    };
  },
  computed: {
    ...mapGetters(['elements'])
  },
  created() {
    this.initDate()
    getChannels().then(res => {
      if (res.status === 200 && res.data && res.data.length > 0) {
        this.channelList = this.channelList.concat(res.data)
        this.listQuery.channel = this.channelList[0].channel
        if (this.channelList.length > 0) {
          this.handleChannelChange(this.channelList[0].channel)
        }
        this.getList()
      }
    })
  },
  methods: {
    cashFormatter(row, col, cellValue) {
      return formatCash(cellValue)
    },
    // 渠道联动
    handleChannelChange(val) {
      const data = {
        channel: val
      }
      getCustomer(data).then(res => {
        this.serviceId = ''
        this.serviceIds = []
        if (res.status === 200) {
          this.serviceIds = res.data
        }
      })
    },
    getList() {
      if (this.listQuery.startDate === '' || this.listQuery.endDate === '') {
        this.$message({
          showClose: true,
          message: this.$t('customerService.dateRangeError'),
          type: 'warning'
        });
        return false
      }

      this.listLoading = true;
      page(this.listQuery).then(res => {
        this.listLoading = false
        if (res.status === 200) {
          this.tableData = res.data.rows
          this.total = parseInt(res.data.total)
        }
      })
    },
    // 查询
    search() {
      this.listQuery.page = 1;
      this.getList();
    },
    // 重置
    reset() {
      this.listQuery.limit = 5;
      this.listQuery.page = 1;
      this.listQuery.channel = this.channelList[0].channel
      this.listQuery.orderId = ''
      this.listQuery.userPhone = ''
      this.listQuery.userName = ''
      this.initDate()
    },
    // 日期初始化
    initDate() {
      this.listQuery.startDate = this.GetDateStr(-7);
      this.listQuery.endDate = this.GetDateStr(-1);
    },
    GetDateStr(AddDayCount) {
      const dd = new Date();
      dd.setDate(dd.getDate() + AddDayCount);// 获取AddDayCount天后的日期
      const y = dd.getFullYear();
      const m = (dd.getMonth() + 1) < 10 ? '0' + (dd.getMonth() + 1) : (dd.getMonth() + 1); // 获取当前月份的日期，不足10补0
      const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate(); // 获取当前几号，不足10补0
      return y + '-' + m + '-' + d;
    },
    // 选中的数据
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 指定代理人
    assignedTo() {
      if (this.multipleSelection.length <= 0) {
        this.$message({
          showClose: true,
          message: this.$t('approvalDispatch.msg1'),
          type: 'warning'
        });
      } else {
        this.dialogFormVisible = true
      }
    },
    // 指定处理人确认按钮
    confirmFn() {
      if (this.serviceId !== '') {
        const cashoutCusviceIds = []
        const customId = this.serviceId
        let customName = ''
        this.multipleSelection.forEach(item => {
          cashoutCusviceIds.push(item.cashoutCusviceId)
        })
        this.serviceIds.forEach(item => {
          if (item.label === customId) {
            customName = item.value
          }
        })
        sendOrder(customId, customName, cashoutCusviceIds).then(res => {
          if (res.status === 200) {
            this.$message({
              showClose: true,
              message: this.$t('approvalDispatch.opersuccess'),
              type: 'success'
            })
            this.dialogFormVisible = false
            this.serviceId = ''
            this.getList()
          }
        })
      } else {
        this.$message({
          showClose: true,
          message: this.$t('rule.pleAssignTo'),
          type: 'warning'
        });
      }
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    }
  }
};
</script>

<style scoped lang='scss'>
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before{
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px  0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 150px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px  0;
    b {
      margin: 0 10px;
      font-weight: normal;
      font-size: 12px;
      color: #5f5f61;
    }
    div {
       width: 150px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.handler {
  width: 90%;
  display: flex;
  justify-content: space-between;
  align-items: center;
    span {
      display: inline-block;
      width: 80px;
      text-align: center;
      font-size: 14px;
      font-weight: bold;
    }
    div:nth-child(2) {
      flex: 1;
    }
}
.menu-list {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.bg {
  display: inline-block;
  border-radius: 5px;
  border: 2px solid #fff;
  width: 25px;
  height: 25px;
  cursor: pointer;
}
.color1 {
  background: #ec7070;
}
.color2 {
  background: #73d466;
}
.type-marker {
  display: flex;
  align-items: center;
  margin-right: 20px;
}
</style>